package scsc.pm;

import scsc.pub.db.DBManage;
import java.util.ArrayList;
import scsc.pub.db.PageListData;
import scsc.pub.exception.UnifyUserException;
import scsc.pub.tools.Logger;
import scsc.web.oa.news.data.NewsData;
import scsc.pub.data.New;
import scsc.pub.data.UserContext;

import java.util.List;


public class NewsOperation {

	public void createNews(DBManage dbmanage, NewsData newsData)
			throws UnifyUserException {
		New data = new New();
		data.setNewsId(newsData.getNewsId());
		data.setNewsTitle(newsData.getNewsTitle());
		data.setClassId(newsData.getClassId());
		data.setContent(newsData.getContent());
		data.setCreator(newsData.getCreator());
		data.setCreateDate(newsData.getCreateDate());
		data.setIsvisible(newsData.getIsvisible());
		if (!newsData.getOpdate().equals("")) {
			data.setOpdate(newsData.getOpdate());
		} else {
			data.setOpdate(null);
		}
		data.setHits(new Integer(0));
		data.setNote(newsData.getNote());
		data.setAssoId(newsData.getAssoId());
		data.setWebsite(newsData.getWebsite());
		dbmanage.sqlSave(data);
		newsData.setNewsId(data.getNewsId());
	}

	public void deleteNews(DBManage dbmanage, String newsId)
			throws UnifyUserException {
		String sqlStr = "delete from scsc.pub.data.New where newsId=" + newsId;
		dbmanage.sqlDelete(sqlStr);
	}

	public void modifyNews(DBManage dbmanage, NewsData newsData)
			throws UnifyUserException {
		New data = new New();
		data.setNewsId(newsData.getNewsId());
		data.setNewsTitle(newsData.getNewsTitle());
		data.setClassId(newsData.getClassId());
		data.setContent(newsData.getContent());
		data.setCreator(newsData.getCreator());
		data.setCreateDate(newsData.getCreateDate());
		data.setIsvisible(newsData.getIsvisible());
		if (!newsData.getOpdate().equals("")) {
			data.setOpdate(newsData.getOpdate());
		} else {
			data.setOpdate(null);
		}
		data.setHits(newsData.getHits());
		data.setNote(newsData.getNote());
		data.setAssoId(newsData.getAssoId());
		data.setWebsite(newsData.getWebsite());
		dbmanage.sqlUpdate(data);
	}

	public void selectNews(UserContext userContext,DBManage dbmanage, String classId, String newsTitle,
			String content, String creator, String isVisible,
			PageListData footer) {
		String condition = " and news.classId=newsClass.classId and news.creator=operator.operatorid";
		if (classId.length() > 0 && !(classId.equals("blank"))) {
			condition = condition + " and news.classId ='" + classId + "'";
		}
		if (newsTitle.length() > 0) {
			condition = condition + " and news.newsTitle like '%" + newsTitle
					+ "%'";
		}
		if (content.length() > 0) {
			condition = condition + " and news.content like '%" + content
					+ "%'";
		}
		if (creator.length() > 0) {
			condition = condition + " and news.creator like '%" + creator
					+ "%'";
		}
		if (isVisible.length() > 0) {
			condition = condition + " and news.isvisible like '%" + isVisible
					+ "%'";
		}
		String sqlStr = "select new scsc.web.oa.news.data.NewsData(news.newsId,news.newsTitle,"
				+ "news.classId,newsClass.className,news.content,news.creator,news.createDate,"
				+ "news.isvisible,news.opdate,news.isvisible,operator.operatorname,news.hits,"
				+ "news.note,news.assoId,news.newsTitle,news.website) "
				+ "from scsc.pub.data.New as news,scsc.pub.data.NewsClass as newsClass,scsc.pub.data.Operator as operator where "

				+ "news.creator='"
				+ userContext.getOperatorData().getOperatorid()
				+ "'  "
				+ condition
				+ "  order by news.opdate DESC,news.classId ASC,news.isvisible ASC";
		dbmanage.sqlQueryPage(sqlStr, footer);
	}

	public ArrayList selectNews(DBManage dbmanage, Integer newsId) {
		String condition = " where news.classId=newsClass.classId and news.creator=operator.operatorid";
		if (String.valueOf(newsId).length() > 0) {
			condition = condition + " and newsId=" + newsId;
		}
		String sqlStr = "select new scsc.web.oa.news.data.NewsData(news.newsId,news.newsTitle,news.classId,"
				+ "newsClass.className,news.content,news.creator,news.createDate,news.isvisible,"
				+ "news.opdate,news.isvisible,operator.operatorname,news.hits,news.note,news.assoId,"
				+ "news.newsTitle,news.website) from scsc.pub.data.New as news,scsc.pub.data.NewsClass as newsClass,scsc.pub.data.Operator as operator"
				+ condition;
		List result = dbmanage.sqlQuery(sqlStr);
		return (ArrayList) result;
	}

	public ArrayList selectNews(DBManage dbmanage, String classId,
			String newsTitle, String content, String creator, String isVisible) {
		String condition = " where news.classId=newsClass.classId and news.creator=operator.operatorid";
		if (classId.length() > 0 && !(classId.equals("blank"))) {
			condition = condition + " and news.classId like '%" + classId
					+ "%'";
		}
		if (newsTitle.length() > 0) {
			condition = condition + " and news.newsTitle like '%" + newsTitle
					+ "%'";
		}
		if (content.length() > 0) {
			condition = condition + " and news.content like '%" + content
					+ "%'";
		}
		if (creator.length() > 0) {
			condition = condition + " and news.creator like '%" + creator
					+ "%'";
		}
		if (isVisible.length() > 0) {
			condition = condition + " and news.isvisible like '%" + isVisible
					+ "%'";
		}
		String sqlStr = "select new scsc.web.oa.news.data.NewsData(news.newsId,news.newsTitle,news.classId,newsClass.className,news.content,news.creator,news.createDate,news.isvisible,news.opdate,news.isvisible,operator.operatorname,news.hits,news.note,news.assoId,news.newsTitle,news.website) from scsc.pub.data.New as news,scsc.pub.data.NewsClass as newsClass,scsc.pub.data.Operator as operator"
				+ condition
				+ " order by news.opdate DESC,news.classId ASC,news.isvisible ASC";
		List result = dbmanage.sqlQuery(sqlStr);
		return (ArrayList) result;
	}

	public ArrayList selectAllNews(DBManage dbmanage, String classId,
			String newsTitle, String content, String creator, String isVisible) {
		String condition = " where news.classId=newsClass.classId and news.creator=operator.operatorid ";
		if (classId.length() > 0 && !(classId.equals("blank"))) {
			condition = condition + " and news.classId = '" + classId + "'";
		}
		if (newsTitle.length() > 0) {
			condition = condition + " and news.newsTitle like '%" + newsTitle
					+ "%'";
		}
		if (content.length() > 0) {
			condition = condition + " and news.content like '%" + content
					+ "%'";
		}
		if (creator.length() > 0) {
			condition = condition + " and news.creator like '%" + creator
					+ "%'";
		}
		if (isVisible.length() > 0) {
			condition = condition + " and news.isvisible like '%" + isVisible
					+ "%'";
		}
		String sqlStr = "select new scsc.web.oa.news.data.NewsData(news.newsId,news.newsTitle,news.classId,"
				+ "newsClass.className,news.content,news.creator,news.createDate,news.isvisible,news.opdate,"
				+ "news.isvisible,operator.operatorname,news.hits,news.note,news.assoId,news.newsTitle,news.website) "
				+ "from scsc.pub.data.NewsClass as newsClass,scsc.pub.data.Operator as operator,"
				+ "scsc.pub.data.New as news "
				+ condition
				+ " order by news.opdate DESC,news.classId ASC,news.isvisible ASC";
		List result = dbmanage.sqlQuery(sqlStr);
		return (ArrayList) result;
	}

	public void selectAllNews(DBManage dbmanage, String classId,
			String newsTitle, String content, String creator, String isVisible,
			PageListData footer) {
		String condition = " where news.classId=newsClass.classId and news.creator=operator.operatorid ";
		if (classId.length() > 0 && !(classId.equals("blank"))) {
			condition = condition + " and news.classId = '" + classId + "'";
		}
		if (newsTitle.length() > 0) {
			condition = condition + " and news.newsTitle like '%" + newsTitle
					+ "%'";
		}
		if (content.length() > 0) {
			condition = condition + " and news.content like '%" + content
					+ "%'";
		}
		if (creator.length() > 0) {
			condition = condition + " and news.creator like '%" + creator
					+ "%'";
		}
		if (isVisible.length() > 0) {
			condition = condition + " and news.isvisible like '%" + isVisible
					+ "%'";
		}
		String sqlStr = "select new scsc.web.oa.news.data.NewsData(news.newsId,news.newsTitle,news.classId,"
				+ "newsClass.className,news.content,news.creator,news.createDate,news.isvisible,news.opdate,"
				+ "news.isvisible,operator.operatorname,news.hits,news.note,news.assoId,news.newsTitle,news.website) "
				+ "from scsc.pub.data.NewsClass as newsClass,scsc.pub.data.Operator as operator,"
				+ "scsc.pub.data.New as news "
				+ condition
				+ " order by news.opdate DESC,news.classId ASC,news.isvisible ASC";
		dbmanage.sqlQueryPage(sqlStr, footer);
	}

	public ArrayList getNewsFiles(DBManage dbmanage, String classId,
			String assoId, String sort, String tpdetail) {
		String sqlStr = "select new scsc.web.system.data.DocManageData(dm.docSeq,dm.docName,dm.shortDoc,dm.sort,sn.dictItemName,dm.tpdetail,tdn.dictItemName,dm.assoId,dm.fileName,dm.fileDir,dm.fileSize,dm.fileState,dm.uptime,dm.docNote)"
				+ " from scsc.pub.data.DocManage as dm,scsc.pub.data.SysDictItem sn,scsc.pub.data.SysDictItem tdn,scsc.pub.data.New as news"
				+ " where dm.assoId=news.assoId  and dm.sort=sn.dictItemId and sn.dictId='000080' and dm.tpdetail=tdn.dictItemId and tdn.dictId='000081'";
		if (classId != null && classId.trim().length() > 0)
			sqlStr = sqlStr + " and news.classId='" + classId + "'";
		if (sort != null && sort.trim().length() > 0)
			sqlStr = sqlStr + " and dm.sort='" + sort + "'";
		if (tpdetail != null && tpdetail.trim().length() > 0)
			sqlStr = sqlStr + " and dm.tpdetail='" + tpdetail + "'";
		if (assoId != null && assoId.trim().length() > 0)
			sqlStr = sqlStr + " and dm.assoId='" + assoId + "'";
		ArrayList result = (ArrayList) dbmanage.sqlQuery(sqlStr);
		return result;
	}

	public void queryNews(DBManage dbmanage, String newsTitle, String content,
			PageListData footer) {
		String condition = " where news.classId=newsClass.classId and news.creator=operator.operatorid";

		if (newsTitle != null && newsTitle.trim().length() > 0
				&& (content == null || content.trim().equals(""))) {
			condition = condition + " and news.newsTitle like '%" + newsTitle
					+ "%'";
		}
		if (content != null && content.trim().length() > 0
				&& (newsTitle == null || newsTitle.trim().equals(""))) {
			condition = condition + " and news.content like '%" + content
					+ "%'";
		}
		if (newsTitle != null && newsTitle.trim().length() > 0
				&& content != null && content.trim().length() > 0) {
			condition = condition + " and ( news.newsTitle like '%" + newsTitle
					+ "%' or news.content like '%" + content + "%')";
		}
		String sqlStr = "select new scsc.web.oa.news.data.NewsData(news.newsId,news.newsTitle,"
				+ "news.classId,newsClass.className,news.content,news.creator,news.createDate,"
				+ "news.isvisible,news.opdate,news.isvisible,operator.operatorname,news.hits,"
				+ "news.note,news.assoId,news.newsTitle,news.website) "
				+ "from scsc.pub.data.New as news,scsc.pub.data.NewsClass as newsClass,scsc.pub.data.Operator as operator"
				+ condition
				+ " and news.isvisible='1'"
				+ " order by news.opdate DESC,news.classId ASC,news.isvisible ASC";
		dbmanage.sqlQueryPage(sqlStr, footer);
	}
}
